package com.qidian.md.service.impl;

import com.qidian.md.mapper.ClientMapper;
import com.qidian.md.service.ClientService;
import com.qidian.mes.pojo.md.Client;
import com.qidian.mes.util.pojo.MESResult;
import com.qidian.mes.util.pojo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/26 14:35
 * @Description
 */
@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    ClientMapper clientMapper;
    @Override
    public MESResult queryByPage(Client client, int page, int limit) {
        Pager<Client> pager=new Pager<>();
        pager.setCurr(page);
        pager.setLimit(limit);
        pager.setTotal( clientMapper.selectCount(client));
        pager.setData(clientMapper.selectByPage(client,(page-1)*limit,limit));
        pager.setMaxPageNum((int) Math.ceil(pager.getTotal()*1.0/limit));
        return MESResult.ok(pager);

    }

    /**
     * 添加客户,先判断客户名是否重复,再添加
     * @param client
     * @return
     */
    @Override
    public MESResult addClient(Client client) {
//
        return MESResult.updateIsOk(clientMapper.addClient(client));
    }


    /**
     * 先根据Id查询内容,返回给前端的编辑页面
     * @param clientId
     * @return
     */
    @Override
    public MESResult queryById(Long clientId) {
        Client client = null;
        if ((client = clientMapper.selectById(clientId)) != null){
            return MESResult.ok(client);
        }
        return MESResult.empty();
    }
    /**
     * 根据客户id-->客户修改编辑
     * 直接更新数据,不用判断客户名是否重复,编码是不一样的
     * @param client
     * @return
     */
    @Override
    public MESResult update(Client client) {

        return MESResult.updateIsOk(clientMapper.update(client));

    }

    /**
     * 单行删除功能
     * @param clientId
     * @return
     */
    @Override
    public MESResult deleteUpdateStatus(Long clientId) {
        return MESResult.ok(clientMapper.deleteUpdateStatus(clientId));
    }


    /**
     * 选中多行删除功能
     * @param clientId
     * @return
     */
    @Override
    public MESResult deleteUpdateSelect(Long[] clientId) {
        int count=clientMapper.deleteUpdateSelect(clientId);
        return MESResult.ok(count+" ");
    }

}
